package com.nit.ehcache.web;

import com.nit.ehcache.dto.WeatherDto;
import com.nit.ehcache.service.WeatherManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Collection;


@RestController
@RequestMapping("api")
public class WeatherAPIContoller {

    @Autowired
    WeatherManager weatherManager;
    /**
     * 天气数据
     * @param id
     * @return
     */

    @Resource
    CacheManager cacheManager;
    @RequestMapping(value = "city/{id:1[0-9]{8}}", method = RequestMethod.GET)
    public WeatherDto loadApi(@PathVariable("id") String id){


        WeatherDto byId = weatherManager.getById(id);

        Collection<String> cacheNames = cacheManager.getCacheNames();
        for (String cacheName : cacheNames) {
            System.out.println(cacheManager.getCache(cacheName).get(id).get().toString());
        }
        return byId;

    }
}
